<?php
error_reporting (E_ALL ^ E_NOTICE);

ob_start("ob_gzhandler");

$start = getTime();
$name = md5($_SERVER["QUERY_STRING"]);

require_once 'password.php';
$dbHost = "mysqlserver";
$link = @mysql_connect($dbHost, $dbUser, $dbPass);
if (!$link)
{
   echo(mysql_error());
   exit;
}
mysql_select_db("renjune_renju");

$table_prefix = "rtg_";
$table_player = $table_prefix . "player";
$table_log = $table_prefix . "log";
$table_rating = $table_prefix . "trn_rating";
$table_tournament = $table_prefix . "trn";
$table_game = $table_prefix . "game";
$table_country = $table_prefix . "country";
$table_city = $table_prefix . "city";

$table_admin = $table_prefix . "admin";
$table_admin_log = $table_prefix . "admin_log";

function ShowProfileTime()
{
    global $enable_profiling;
    global $start;
    if (isset($enable_profiling) && $enable_profiling != 0)
    {
        $end = getTime();
        echo '<div style="text-align:right"><small>Time: ', ($end-$start), ' sec.</small></div>';
    }
    
}

if (!file_exists("password.php"))
{
    echo("password.php not found, create it manually and define \$dbUser and \$dbPass vars");
    exit(0);    
}

function err($text, $error_title = "")
{
    $h = fopen ("err_log", "a+");
    if ($h !== FALSE)
    {
        fwrite($h, $text . "\n");
        fclose($h);
    }
    if ($error_title == "")
        $error_title = "OOPS! You broke it.";
    $error_text = htmlentities($text);
    require 'tpl/error.tpl';
    exit;
}

function cycle($val1, $val2)
{
    static $curr = true;
    $curr = !$curr;
    return $curr ? $val2 : $val1;
}

function sortbyrating($a, $b)
{
    if ($a->player_end_rating == $b->player_end_rating)
        return 0;
    return $a->player_end_rating < $b->player_end_rating ? 1 : -1;
    
}

function &get_pdict($cond)
{
    global $table_player;
    global $table_log;
    global $table_rating;
    global $table_tournament;
    global $table_game;
    global $table_country;
    global $table_city;
    if ($cond == "")
        $cond = "0";
    
    $q = "SELECT * FROM $table_player 
        LEFT JOIN $table_country ON $table_country.country_id = $table_player.country_id
        LEFT JOIN $table_city ON $table_city.city_id = $table_player.city_id
        WHERE $cond";
    $pdict = Array();
    $result = mysql_query($q);
    while ($row = mysql_fetch_object($result))
    {
        $pdict[$row->player_id] = $row;
    }
    return $pdict;
}

function &get_plist($country_id, $date)
{
    global $table_player;
    global $table_log;
    global $table_rating;
    global $table_tournament;
    global $table_game;
    global $table_country;
    global $table_city;
    
    global $show_datesec;
    global $max_datesec;
    
    $q = "";
    $country_q = "";
    if ($country_id > 0)
    {
        $country_q = " AND $table_player.country_id=$country_id ";    
    }
    
    $t5years = $show_datesec-5*31557600;
    $t2years = $show_datesec-2*31557600;
    $t1years = $show_datesec-1*31557600;
    $q = "SELECT * 
        FROM $table_rating
        LEFT JOIN $table_player ON $table_player.player_id = $table_rating.player_id
        LEFT JOIN $table_country ON $table_country.country_id = $table_player.country_id
        LEFT JOIN $table_city ON $table_city.city_id = $table_player.city_id
        WHERE datesec >= $t5years AND datesec <= $show_datesec $country_q
        ORDER BY datesec DESC";

    $result = mysql_query($q);
    $pdict = Array();
   
    while ($row = mysql_fetch_object($result))
    {
        $id = $row->player_id;
        if (!isset($pdict[$id]))
        {
            $pdict[$id] = $row;
            $pdict[$id]->g5 = 0;
            $pdict[$id]->g2 = 0;
            $pdict[$id]->g1 = 0;
        }
        if ($row->g == 0)
            continue;
        $pdict[$id]->g5 += $row->g;
        if ($row->datesec >= $t2years)
             $pdict[$id]->g2 += $row->g;
        if ($row->datesec >= $t1years)
             $pdict[$id]->g1 += $row->g;
    }
    
//    echo $q;
//    exit;
    
    $plist = Array();
    
    $rank = 0;
    $rank_step = 1;
    $prev_rating = 0;
    $max_datesec = 0;

    $arr_tmp = array_values($pdict);
    usort($arr_tmp, "sortbyrating");
    
    foreach ($arr_tmp as $row)
    {
        if ($row->datesec > $max_datesec)
        {
            $max_datesec = $row->datesec;       
        }
        
        if (!$row->is_established || $row->g5 == 0)
        {
            $row->rank = 0;
            //array_push($plist, $row);
            continue;
        }
        
        $rating = floor($row->player_end_rating);
        if ($prev_rating != $rating)
        {
            $rank += $rank_step;
        }
        else
        {
            $rank_step++;
        }
        
        $row->rank = $rank;
        
        array_push($plist, $row);
    }
    return $plist;
}

function password_from_string($str)
{
    return crypt($str, "k7y");
}

function getTime() {
    $timer = explode( ' ', microtime() );
    $timer = $timer[1] + $timer[0];
    return $timer;
} 

?>
